package com.salim.leetcode.$1;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-7-15
 * Time: 下午11:42
 * To change this template use File | Settings | File Templates.
 */
public class TwoSum {

    Map<Integer,Integer> elementMap = new HashMap<Integer,Integer>();

    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[]{0,0};
        for(int i=0;i<nums.length;i++){
            int x = nums[i];
            int y = target - nums[i];

            //初始化数据
            if(!elementMap.containsKey(x)){
                elementMap.put(x,i);
            }

            //查找另一个值
            if(elementMap.containsKey(y)){
                Integer yIndex = elementMap.get(y);
                if(yIndex != i){
                    result[0] = yIndex + 1;
                    result[1] = i + 1;
                    break;
                }
            }
        }
        return result;
    }

    class Node{
        Integer y = null;
        Integer index = null;

        Node(Integer y,Integer index){
            this.y = y;
            this.index = index;
        }
    }

}
